<template>
  <div class="app-container">
    <el-form
      ref="teacherForm"
      :model="teacher"
      label-width="120px"
      :rules="rules"
    >
      <el-form-item label="讲师名称" prop="name">
        <el-input
          v-model="teacher.name"
          placeholder="请输入讲师名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="入驻时间" prop="joinDate">
        <el-date-picker
          v-model="teacher.joinDate"
          placeholder="请输入入驻时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="讲师排序" prop="sort">
        <el-input-number
          v-model="teacher.sort"
          placeholder="请输入讲师排序"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="讲师头衔" prop="level">
        <el-select v-model="teacher.level" placeholder="请选择头衔">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历" prop="career">
        <el-input
          v-model="teacher.career"
          placeholder="请输入讲师资历"
        ></el-input>
      </el-form-item>
      <el-form-item label="讲师简介" prop="intro">
        <el-input
          v-model="teacher.intro"
          placeholder="讲师简介"
          type="textarea"
          :rows="10"
        ></el-input>
      </el-form-item>
      <!-- 讲师头像 todo -->
      <el-form-item label="头像" prop="avatar">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:9110/aliyun-oss/admin/aliyun/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate('teacherForm')"
          >确认</el-button
        >
        <!-- <el-button type="primary" @click="resetForm('teacherForm')"
          >重置</el-button
        > -->
      </el-form-item>
      <!-- <el-form-item label="测试">
        <img :src="teacher.avatar" alt="" style="width: 500px;height: 500px;" />
      </el-form-item> -->
    </el-form>
  </div>
</template>
<script>
import teacher from "../../api/teacher";
export default {
  data() {
    return {
      teacher: {},
      saveBtnDisabled: false,
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        joinDate: [{ required: true, message: "必填", trigger: "change" }],
        sort: [{ required: true, message: "必填", trigger: "blur" }],
        level: [{ required: true, message: "必填", trigger: "blur" }],
        career: [{ required: true, message: "必填", trigger: "blur" }],
        intro: [{ required: true, message: "必填", trigger: "blur" }],
        avatar: [{ required: true, message: "必填", trigger: "blur" }],
      }, //表单提交状态 按钮可用
    };
  },
  mounted() {
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id);
    }
  },
  methods: {
    /**
     * 根据id查询教师
     * @param {*} id
     */
    fetchDataById(id) {
      teacher.getById(id).then((resp) => {
        if (resp) {
          this.teacher = resp.data.item;
        }
      });
    },
    save(teacherForm) {
      this.$refs.teacherForm.validate((valid) => {
        if (valid) {
          teacher.save(this.teacher).then((resp) => {
            if (resp) {
              // this.saveBtnDisabled = false;
              this.$message.success(resp.message);
              this.$router.push("/teacher/list");
            }
          });
        } else {
          this.$message.error("请输入必填项！");
          return false;
        }
      });
    },
    /**
     * 添加教师
     */
    saveOrUpdate(teacherForm) {
      this.$refs[teacherForm].validate((valid) => {
        if (valid) {
          if (this.teacher.id) {
            this.Update();
          } else {
            this.save();
          }
        } else {
          this.$message.error("请输入必填项！");
          return false;
        }
      });
    },
    Update(teacherForm) {
      this.$refs.teacherForm.validate((valid) => {
        if (valid) {
          teacher.updateById(this.teacher).then((resp) => {
            if (resp) {
              this.saveBtnDisabled = false;
              this.$message.success(resp.message);
              this.$router.push("/teacher/list");
            }
          });
        } else {
          this.$message.error("请输入必填项！");
          return false;
        }
      });
    },
    /**
     * 文件上传成功
     * @param {*} res
     * @param {*} file
     */
    handleAvatarSuccess(res, file) {
      // this.teacher.avatar = res.data.url;
      // console.log(file.row);
      // this.teacher.avatar = URL.createObjectURL(file.raw);
      this.teacher.avatar = res.data.url;
      // console.log(this.teacher.avatar);
      // console.log(res.data.url);
      this.$forceUpdate();
    },
    /**
     * 文件上传之前的钩子校验函数
     * @param {*} file
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // resetForm(teacherForm) {
    //   this.$refs[teacherForm].resetFields();
    // },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
